const fetch = require('../../request/fetch')
// 定义一个总毫秒数，以一分钟为例。TODO，传入一个时间点，转换成总毫秒数
var total_micro_second = 6000000;

/* 毫秒级倒计时 */
function count_down(that) {
  // 渲染倒计时时钟
  that.setData({
    clock: date_format(total_micro_second)
  });

  if (total_micro_second <= 0) {
    that.setData({
      clock: "已经截止"
    });
    // timeout则跳出递归
    return;
  }
  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 10;
    count_down(that);
  }, 10)
}

// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  // 秒位
  var sec = fill_zero_prefix((second - hr * 3600 - min * 60)); // equal to => var sec = second % 60;
  return hr + ":" + min + ":" + sec + " ";
}

// 位数不足补零
function fill_zero_prefix(num) {
  return num < 10 ? "0" + num : num
}

Page({
  data: {
    title: '', // 单前题目
    optionList: [], // 当前题数
    type: '', //题目类型
    currentTopicNum: "", // 当前题数
    id: '', // 当前题目的id
    choice: '', // 选中的答案
    checkbox: '', // 多选选择的答案
    currentScore: '', //当前分数
    res: '', // 正确的答案
    right: '', // 学生选择的答案
    deadline: '', // 截取时间
    myindex: null, // 选中的索引值
    clock: '', // 时间
    currentScore: 0, //当前成绩
    next: 0, //下一页
    submitLoading: false,
    page: 1, // 下一页数
    selection: '',
    zxcv: []
  },
  
  onLoad: function (options) {
    count_down(this);
    this.getTopicFn()
    this.time()
  },

  time(){
    fetch.setUserInfo({
      data:{
        startTime: this.data.clock
      }
    }).then(res => {
      wx.setStorageSync("time",this.data.clock)
    })
  },

  //获取题目信息
  getTopicFn() {
    fetch.getTopic({
      data: {
        num: this.data.page
        // num: 65
      }
    }).then(res => {
      console.log(res.data);
      for (let i = 0; i < res.data.optionList.length; i++) {
        res.data.optionList[i].isBoole = false
        res.data.optionList[i].ids = false
      }
      let {
        currentTopicNum,
        id,
        optionList,
        type,
        title
      } = res.data;
      let num = this.data.page + 1;
      this.setData({
        currentTopicNum,
        id,
        optionList,
        title,
        type,
        page: num
      })
    })
  },

  // 单选题选中的
  checked(e) {
    let index = e.currentTarget.dataset.index
    let item = this.data.optionList[index]
    item.isBoole = !item.isBoole
    this.setData({
      optionList: this.data.optionList
    })
    let choice = this.data.optionList[index].choice.charAt(0)
    let newArr = []
    for (let k = 0; k < this.data.optionList.length; k++) {
      if (this.data.optionList[k].isBoole == true) {
        newArr.push(this.data.optionList[k].choice.charAt(0))
      }
    }
    this.setData({
      myindex: index,
      choice: choice, // 单选
      checkbox: newArr.toString()
    })
  },
  // 单选确认按钮
  confirmBtn() {
    if (this.data.choice == '') {
      wx.showToast({
        title: '请选择你的答案',
        icon: 'success',
        duration: 2000,
      })
    } else {
      fetch.judgeTopicAnswer({
        data: {
          answer: this.data.choice,
          num: this.data.currentTopicNum,
          topicId: this.data.id
        }
      }).then(res => {
        let {
          currentScore,
          right
        } = res.data
        let myres = res.data.res
        myres = right
        if (myres == "D") {
          this.setData({
            selection: 3
          })
        } else if (myres == "A") {
          this.setData({
            selection: 0
          })
        } else if (myres == "B") {
          this.setData({
            selection: 1
          })
        } else if (myres == "C") {
          this.setData({
            selection: 2
          })
        }

        this.setData({
          currentScore,
          res: myres,
          right,
          next: 1,
        })
      })
    }

  },
  // 多选确认按钮
  checkBtn() {
    if (this.data.choice == '') {
      wx.showToast({
        title: '请选择你的答案',
        icon: 'success',
        duration: 2000,
      })
    } else {
      fetch.judgeTopicAnswer({
        data: {
          answer: this.data.checkbox,
          num: this.data.currentTopicNum,
          topicId: this.data.id
        }
      }).then(res => {
        let {
          currentScore,
          right
        } = res.data
        let myres = res.data.res

        let ghjk = [];
        if (right.indexOf("A") != -1) {
          ghjk[0] = true
        } else {
          ghjk[0] = false
        }

        if (right.indexOf("B") != -1) {
          ghjk[1] = true
        } else {
          ghjk[1] = false
        }

        if (right.indexOf("C") != -1) {
          ghjk[2] = true
        } else {
          ghjk[2] = false
        }

        if (right.indexOf("D") != -1) {
          ghjk[3] = true
        } else {
          ghjk[3] = false
        }
        console.log(ghjk);
        this.setData({
          currentScore,
          res: myres,
          right,
          next: 1,
          zxcv: ghjk
        })
      })
    }
  },
  // 下一题按钮
  nextTo() {
    this.setData({
      next: 0,
      myindex: null
    })
    this.getTopicFn()
  },
  onHide() {
    console.log('我关闭了');
    console.log(this.data.listData);
    for (let i = 0; i < this.data.listData.length; i++) {
      this.setData({
        deadline: this.data.listData[i].countDown
      })
    }
    console.log(this.data.deadline);
  },
})